
import sys
from random import random
from elasticsearch import Elasticsearch, helpers

class ESUtility():
    def __init__(self, hosts=None, **kwargs):
        from elasticsearch import Elasticsearch
        self.es = Elasticsearch(hosts=hosts, **kwargs)


def create_index(index_name):
    print("begin create index")
    setting = {
        "settings": {
            "number_of_replicas": 0,
            "number_of_shards": 5
        },

        "settings":{
        "number_of_shards" : "5",
        "blocks" : {
          "read_only_allow_delete" : "false"
        },
        "analysis" : {
          "analyzer" : {
            "i_analyzer" : {
              "filter" : [
                "lowercase"
              ],
              "tokenizer" : "i_pinyin"
            }
          },
          "tokenizer" : {
            "i_pinyin" : {
              "trim_whitespace " : "true",
              "lowercase" : "true",
              "keep_original" : "true",
              "keep_joined_full_pinyin " : "true",
              "remove_duplicated_term" : "true",
              "keep_first_letter" : "true",
              "keep_separate_first_letter" : "false",
              "type" : "pinyin",
              "limit_first_letter_length" : "16",
              "keep_full_pinyin" : "false"
            }
          }
        },
        "number_of_replicas" : "1",
      },
        "mappings":{
            "properties":{
                "basedataId" : {
            "type" : "long"
          },
          "dicId" : {
            "type" : "long"
          },
          "dicValueId" : {
            "type" : "long"
          },
          "eid" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "number" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "value" : {
            "type" : "text",
            "fields" : {
              "ik" : {
                "type" : "text",
                "analyzer" : "ik_max_word"
              },
              "pinyin" : {
                "type" : "text",
                "analyzer" : "pinyin"
              }
            }
          }
            }
            
        } 
    }
    es_client.indices.create(index=index_name, body=setting)
    print("end create index")

index_name='test_dic_value'
es_client = Elasticsearch(hosts='172.18.8.118', port=9200) 
create_index(index_name)